﻿using HlELock.Core;
using HlELock.Core.Events;
using HlELock.Domain;
using HlELock.Domain.Entities;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Linq;
using System.Windows;

namespace HlELock.Modules.Hotel.Main.ViewModels
{
    public class RoomInfoUserControlViewModel : BindableBase, INavigationAware
    {
        private readonly IDialogService _dialogService;
        public RoomInfoUserControlViewModel(IDialogService dialogService)
        {
            _dialogService = dialogService;
        }

        private CheckInRoom _checkInRoom;
        public CheckInRoom CheckInRoom
        {
            get { return _checkInRoom; }
            set { SetProperty(ref _checkInRoom, value); }
        }
        private CheckIn _checkIn;
        public CheckIn CheckIn
        {
            get { return _checkIn; }
            set { SetProperty(ref _checkIn, value); }
        }

        private DelegateCommand _returnRoomCommand;
        public DelegateCommand ReturnRoomCommand =>
            _returnRoomCommand ?? (_returnRoomCommand = new DelegateCommand(ExecuteReturnRoomCommand));


        /// <summary>
        /// 退房
        /// </summary>
        void ExecuteReturnRoomCommand()
        {
            var cardNo = MyAppContext.CardService.IssueCheckOutCard();

            if (string.IsNullOrWhiteSpace(cardNo))
            {
                // 使用内置的 MessageBox
                MessageBoxResult result = MessageBox.Show(
                    "房卡注销失败，是否继续退房？",
                    "确认",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Question);

                if (result != MessageBoxResult.Yes)
                {
                    return;
                }
            }
            using (var context = new HlELockDbContext())
            {
                var room = context.Rooms.FirstOrDefault(f => f.Id == CurrentRoom.Id);
                if (room != null)
                {
                    room.IsOccupancies = false;
                }
                var checkRoom = context.CheckInRooms.Where(f => f.RoomNumber == room.RoomNumber && f.CheckOutTime == null).ToList();
                foreach (var check in checkRoom)
                {
                    check.CheckOutTime = DateTime.Now;
                }

                context.CardLogs.Add(new CardLog
                {
                     CardAction=CardActionType.注销卡,
                     CardNumber=cardNo,


                });
                context.SaveChanges();
            }
            EventBus.CloseDialog();
            MessageBox.Show(AppHelper.GetResourceString("TuiFangChenGong"));
        }


        private DelegateCommand _sendCardCommand;
        public DelegateCommand SendCardCommand =>
            _sendCardCommand ?? (_sendCardCommand = new DelegateCommand(ExecuteSendCardCommand));

        /// <summary>
        /// 发卡
        /// </summary>
        void ExecuteSendCardCommand()
        {
            var beginTime = CheckIn?.BeginTime ?? DateTime.Today;
            var endTime = CheckIn?.EndTime ?? DateTime.Today.AddDays(1);
            // var cardNumber = SfHelper.Instance.IssueGuestCard(CurrentRoom.RoomNumber, beginTime, endTime);
            var cardNumber = MyAppContext.CardService.IssueCard(CurrentRoom.RoomNumber, beginTime, endTime, CurrentRoom.BuildingNumber, CurrentRoom.FloorNumber);
            if (!string.IsNullOrWhiteSpace(cardNumber))
            {
                using (var context = new HlELockDbContext())
                {
                    context.CardLogs.Add(new CardLog
                    {
                        CardAction = CardActionType.开卡,
                        CardNumber = cardNumber,
                        CardType = CardType.客人卡.ToString(),
                        CreationTime = DateTime.Now,
                        RoomNumber = CurrentRoom.RoomNumber
                    });
                    context.SaveChanges();
                }
            }
            else
            {
                MessageBox.Show(AppHelper.GetResourceString("FaKaShiBai"));
            }


                EventBus.CloseDialog();
            return;
        }



        private Room _room;
        public Room CurrentRoom
        {
            get { return _room; }
            set { SetProperty(ref _room, value); }
        }


        private string _password = "123456";
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return _password; }
            set { SetProperty(ref _password, value); }
        }

        public bool IsNavigationTarget(NavigationContext navigationContext) => true;

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            CurrentRoom = navigationContext.Parameters.GetValue<Room>(nameof(Room));
            using (var context = new HlELockDbContext())
            {
                var cir = context.CheckInRooms.FirstOrDefault(w => w.RoomNumber == CurrentRoom.RoomNumber && w.CheckOutTime == null);
                if (cir == null)
                {

                }
                else
                {
                    var cr = context.CheckIns.Find(cir.CheckInId);

                    CheckInRoom = cir;
                    CheckIn = cr;
                }

            }
        }
    }
}
